package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.util.DateUtils;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl  implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    //体检预约方法
    @Override
    public Result order(Map map) throws Exception{
        //先检查当前日期是否进行了预约
        //获取当前日期
        String orderDate = (String) map.get("orderDate");
        //通过工具类把时间转换成date类型
        Date date = DateUtils.parseString2Date(orderDate);
        //根据预约日期查询预约设置
        OrderSetting orderSetting= orderSettingDao.findByOrderDate(date);
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //检查预约日期是否预约满
        //获取可预约人数
        int number = orderSetting.getNumber();
        //已预约人数
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            //预约满了 不可预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //检查当前用户是否是会员 根据手机号判断
        //获取手机号
        String telephone = (String) map.get("telephone");
        //根据手机号查询是否有这个会员
        Member member=memberDao.findByTelephone(telephone);
        //防止重复预约(同一会员  同一日期 预约同一套餐)
        if (member!=null){
            //获取该会员id
            Integer id = member.getId();
            //获取套餐id
            Integer setmealId = Integer.parseInt((String)map.get("setmealId")) ;
            Order order = new Order(id, date, null, null, setmealId);
            //调用条件查询方法 查询预约信息.
            List<Order> list=orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
                //已经完成预约 不能重复预约 给出提示信息
                return  new Result(false,MessageConstant.HAS_ORDERED);
            }
        } else {
            //当前用户不是会员,需要添加到会员表
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册
        }

         //预约成功
        //保存预约信息到预约表
        //5、预约成功，更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);
        //可以预约,设置预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
      Map map=orderDao.findById(id);
      if (map!=null){
          //处理日期格式
          Date orderDate = (Date) map.get("orderDate");
          map.put("orderDate",DateUtils.parseDate2String(orderDate));
      }
        return map;
    }
}
